import { useState } from "react";
import { storageStore } from "utils";

// https://usehooks.com/useLocalStorage/
function useLocalStorage(key: string, initialValue: any) {
  const [storedValue, setStoredValue] = useState(() => {
    try {
      const value = storageStore.getValue(key);
      return value ?? initialValue;
    } catch (error) {
      console.log(error);
      return initialValue;
    }
  });

  const setValue = (value: any) => {
    try {
      setStoredValue(value);
      storageStore.setValue(key, value);
    } catch (error) {
      console.log(error);
    }
  };

  return [storedValue, setValue];
}

export default useLocalStorage;
